package springbucks.controller;

import cn.hutool.core.util.IdUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springbucks.dto.GradeDTO;
import springbucks.entity.ResultVO;
import springbucks.entity.Student;
import springbucks.service.LeaderBoardService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangyidi
 * @version V1.0
 * @Description 对第一次的项目进行改造 通过 resutlful 和 xml 两种方式对返回数据进行格式处理
 */
@RestController
@RequestMapping(value = "/leaderboard")
public class LeaderBoardController {

    private List<Student> students;

    @Resource
    private LeaderBoardService leaderBoardService;


    /**
     * @Description 排行榜 JSON
     * @author wangyidi
     * @date 2022/11/23 5:06 下午
     * @return ResultVO
     **/
    @GetMapping(value = "/leaderboard_json",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO leaderboardSJON() {
        List<Student> list = leaderBoardService.leaderboard(students);
        return ResultVO.success(list);
    }


    /**
     * @Description 排行榜 XML
     * @author wangyidi
     * @date 2022/11/23 5:06 下午
     * @return ResultVO
     **/
    @GetMapping(value = "/leaderboard_xml",produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    public ResultVO leaderboardXML() {
        List<Student> list = leaderBoardService.leaderboard(students);
        return ResultVO.success(list);
    }



    /**
     * @Description 初始化学生列表
     * @author wangyidi
     * @date 2022/11/23 5:06 下午
     * @return void
    **/
    @PostMapping(value = "/init")
    public ResultVO init () {
        if(students == null) {
            students = new ArrayList<>();
            Student student1 = Student.builder().id(IdUtil.randomUUID()).name("小王").build();
            Student student2 = Student.builder().id(IdUtil.randomUUID()).name("小张").build();
            Student student3 = Student.builder().id(IdUtil.randomUUID()).name("小刘").build();
            students.add(student1);
            students.add(student2);
            students.add(student3);
        }
        return ResultVO.success(students);
    }


    /**
     * @Description 根据学生ID 进行打分
     * @author wangyidi
     * @date 2022/11/23 5:06 下午
     * @return ResultVO
     **/
    @PostMapping(value = "/grade")
    public ResultVO grade(@RequestBody GradeDTO gradeDTO) {
        leaderBoardService.grade(gradeDTO,students);
        return ResultVO.success(students);
    }



}
